
;layui.define(['jquery', 'layer', 'util', 'upload', 'form'], function(exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        n = layui.form,
        s = layui.upload,
        u = layui.util;

    function ajaxF(typeS,urlS,dataS,callBackF) {
        var index;
        $.ajax({
            type: typeS,
            url: urlS,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data:dataS,
            beforeSend:function(){
                index = layer.load();
            },
            success: function(res){
                console.log(res);
                layer.close(index);
                if (res.code === -1 || res.code === "-1"){
                    layer.alert(res.msg, {icon: 2});
                }else if (res.code === 1){
                    if (typeof callBackF === "function"){
                        callBackF(res);
                    }
                }else{
                    console.log(res);
                    layer.alert("出错了，请查看控制台", {icon: 3});
                }
            },
            error:function () {
                layer.close(index);
                layer.alert('网路异常，请求失败', {icon: 1});
            },
            complete:function () {
                try {
                    layer.close(index);
                }catch (e) {
                    console.log(e.message);
                }
            }
        });
    }

    const interactive = {
        post: function (url, data, callback) {
            ajaxF("POST", url, data, callback)
        },
        get: function (url, data, callback) {
            ajaxF("GET", url, data, callback)
        },
        upload: function (elem, url, group, exts, callback) {
            var loadIndex;
            s.render({
                elem: "#"+elem
                ,url: url //必填项
                ,method: 'POST'
                ,accept: 'file' //普通文件
                ,exts: exts
                ,headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
                ,data: {
                    group : group
                }
                ,before: function(obj){ // obj 参数同 choose
                    loadIndex = layer.load(); // 上传 loading
                }
                ,done: function(res){
                    layer.close(loadIndex);
                    //上传完毕回调
                    if (res.code === 1){
                        var data = res.data;
                        $(`#${elem}_icon`).hide();
                        $(`#${elem}_input`).val(data.src);
                        var preview = $(`#${elem}_preview`);
                        preview.attr('src',data.src);
                        preview.show();
                        callback && callback(res.data);
                    }else if (res.code === -1){
                        layer.msg(res.msg);
                    }else {
                        layer.msg("发生未知错误");
                    }
                }
                ,error: function(){
                    layer.close(loadIndex);
                    layer.msg("上传出错了");
                }
            });
        },
    };
    exports('interactive', interactive);
})
